package com.example.archive.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.archive.entity.RestrictedArchiveRequest;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 受限档案访问申请Mapper接口
 * 提供受限档案申请的数据库操作
 */
@Mapper
public interface RestrictedArchiveRequestMapper extends BaseMapper<RestrictedArchiveRequest> {
    
    /**
     * 分页查询受限档案申请，包含关联信息
     * @param page 分页对象
     * @param status 审批状态
     * @param userId 用户ID
     * @param approverId 审批人ID
     * @param search 搜索关键词
     * @return 分页结果
     */
    @Select("<script>" +
            "SELECT " +
            "r.request_id, r.user_id, r.archive_id, r.reason, r.expected_duration, " +
            "r.request_time, r.approver_id, r.approval_status, r.approval_time, r.notes, " +
            "a.title as archive_title, a.archive_type, a.security_level, " +
            "u1.username as user_name, u2.username as approver_name " +
            "FROM restricted_archive_requests r " +
            "LEFT JOIN archives a ON r.archive_id = a.archive_id " +
            "LEFT JOIN users u1 ON r.user_id = u1.user_id " +
            "LEFT JOIN users u2 ON r.approver_id = u2.user_id " +
            "WHERE 1=1 " +
            "<if test='status != null and status != \"\"'>" +
            "AND r.approval_status = #{status} " +
            "</if>" +
            "<if test='userId != null'>" +
            "AND r.user_id = #{userId} " +
            "</if>" +
            "<if test='approverId != null'>" +
            "AND r.approver_id = #{approverId} " +
            "</if>" +
            "<if test='search != null and search != \"\"'>" +
            "AND (a.title LIKE CONCAT('%', #{search}, '%') " +
            "OR r.reason LIKE CONCAT('%', #{search}, '%') " +
            "OR u1.username LIKE CONCAT('%', #{search}, '%')) " +
            "</if>" +
            "ORDER BY r.request_time DESC" +
            "</script>")
    IPage<RestrictedArchiveRequest> selectRequestsWithDetails(
            Page<RestrictedArchiveRequest> page,
            @Param("status") String status,
            @Param("userId") Integer userId,
            @Param("approverId") Integer approverId,
            @Param("search") String search
    );
} 